package com.fastloan.safe.mgt.root.controller;

import com.fastloan.safe.core.perms.Licensing;
import com.fastloan.safe.mgt.root.model.CmDefResMenu;
import com.fastloan.safe.mgt.root.service.CmDefResMenuService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import com.kawins.spring.valid.core.BeanValid;
import com.kawins.spring.valid.group.Group;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author kawins
 *         createTime 2017-10-15 15:26
 */
@RestController
@RequestMapping("/api/web/cmDefResMenu")
public class CmDefResMenuController extends BaseController<CmDefResMenu> {

    @Autowired
    private CmDefResMenuService defResMenuService;

    /**
     * 顶级菜单分页列表
     *
     * @param name
     * @param menuId 菜单ID
     * @param isLast 是否为进入下一级请求 0:顶级,1:上级,2:下级(不是0,1都可以)
     * @return
     */
    @RequestMapping("/pageList")
    @RequiresPermissions("all:/web/cmDefResMenu/pageList")
    @Licensing(value = {"add#post:/web/cmDefResMenu", "update#put:/web/cmDefResMenu", "del#delete:/web/cmDefResMenu/cmDefResMenuId"})
    public ResultTo pageList(@RequestParam(value = "name", required = false) String name,
                             @RequestParam(value = "menuId", required = false) String menuId,
                             @RequestParam(value = "isLast", defaultValue = "0") int isLast) {
        Map<String, Object> map = new HashMap<>();
        if (isLast != 0 && StringUtils.isBlank(menuId)) {
            return new ResultTo(ResultEnum.PARAM_ERROR, "进入上级或下级操作时menuId不能为空");
        }
        map.put("isNext", isLast);
        if (StringUtils.isNotBlank(name)) {
            map.put("name", name);
        }
        return new ResultTo().setData(defResMenuService.pageListNoPage(map, menuId, isLast));
    }

    /**
     * 插入菜单
     *
     * @param defResMenu
     * @return
     */
    @PostMapping
    @BeanValid
    @RequiresPermissions("post:/web/cmDefResMenu")
    public ResultTo insert(@Validated(value = Group.Insert.class) CmDefResMenu defResMenu, BindingResult bindingResult) {
        defResMenuService.insertForShort(defResMenu);
        return new ResultTo();
    }

    /**
     * 　更新菜单数据
     *
     * @param defResMenu
     * @return
     */
    @PutMapping
    @BeanValid
    @RequiresPermissions("put:/web/cmDefResMenu")
    public ResultTo update(@Validated(value = Group.Update.class) CmDefResMenu defResMenu, BindingResult bindingResult) {
        defResMenuService.updateForSort(defResMenu);
        return new ResultTo();
    }

    /**
     * 删除菜单
     *
     * @param cmDefResMenuId
     * @return
     */
    @DeleteMapping("/{cmDefResMenuId}")
    @RequiresPermissions("delete:/web/cmDefResMenu/cmDefResMenuId")
    public ResultTo delete(@PathVariable String cmDefResMenuId) {
        if (adaptService.selectCount(new CmDefResMenu().setParentId(cmDefResMenuId)) != 0) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "请先删除所有子子菜单");
        }
        defResMenuService.deleteAll(cmDefResMenuId);
        return new ResultTo();
    }

    /**
     * 根据用户角色获得菜单信息
     *
     * @return
     */
    @GetMapping(value = "/byRole")
    @RequiresUser
    public ResultTo select() {
        return new ResultTo().setData(defResMenuService.selectMenuListByRole());
    }
}
